package com.zb.test;

import org.junit.Test;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * DateTimeFormatter: 用于格式化日期和时间
 */

public class JAVA8_15_DateTimeFormatter {
    @Test
    public void test1(){
        LocalDateTime localDateTime = LocalDateTime.now();

        //采用系统给定的格式转化
        DateTimeFormatter dtf1 = DateTimeFormatter.ISO_DATE;//只要日期
        String date1 = dtf1.format(localDateTime);
        System.out.println(date1);

        //自定义格式化
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String date2 = dtf2.format(localDateTime);
        System.out.println(date2);

        //将字符串的日期转化为日期对象
        LocalDateTime res = LocalDateTime.parse("2018-04-05 12:12:12",dtf2);//采用dtf2格式转化
        System.out.println(res);

        LocalDate localDate = LocalDate.parse("2018-04-05",DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        System.out.println(localDate);

    }
}
